package net.alche.sdd.fatis.macro;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.alche.al.util.AC;
import net.alche.al.util.M;
import net.alche.fa.core.model.Logic;
import net.alche.fa.core.parse.FaKeyWord;
import net.alche.sdd.fatis.SddFatisMacro;

public class SddFatisMacroSwitch {
  public static final Logger logger = LoggerFactory.getLogger(SddFatisMacroSwitch.class);
  SddFatisMacro macroPar;
  String keyVar = null;
  String varVal = null;
  int continuLv = -1; // 跳过的行
  boolean hasCaseOk = false;

  public SddFatisMacroSwitch(SddFatisMacro macro, String keyVar) {
    this.macroPar = macro;
    this.keyVar = keyVar;
    Object obj = macro.getParam(keyVar);
    if (obj != null) {
      varVal = AC.toStr(obj);
    }
  }

  public boolean doSwitchCaseLine(String line, int lv) {
    if (hasCaseOk) {
      continuLv = lv;
      return false;
    }
    line = line.trim();
    String caseVal = line.substring(14).trim();
    return doSwitchCase(caseVal, lv);
  }

  public boolean doSwitchCase(String val, int lv) {
    if (hasCaseOk)
      return false;
    if (varVal != null && M.eq(varVal, val)) {
      hasCaseOk = true;
      continuLv = -1;
      return true;
    }
    continuLv = lv;
    return false;
  }

  public boolean doSwitchDeft(int lv) {
    if (hasCaseOk) {
      continuLv = lv;
      return false;
    }
    continuLv = -1;
    return true;
  }

  public int getContinueLv() {
    return continuLv;
  }

  public boolean needContinue(int lv) {
    if (getContinueLv() < lv)
      return true;
    return false;
  }
}
